#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/5/9 10:43
@File: Aml__init__.py
"""

from qturboframe.lib.common.web_config import web_config
from qturboframe.lib.test_suite.web_test_flow import WebTestFlow
from qturboframe.lib.utils.variable import Variable
from qturboframe.lib.webdriver.webdriver_factory import WebDriverFactory

from BladeFerm.Lib.Common import logger
from BladeFerm.databases import DB_APP
from HGScripts.HGConfig import SysConfig


class TestFlow(WebTestFlow):
    def __init__(self):
        super().__init__()
        self.browser_id = None
        self.is_blade_flag = False
        self.init_env = "oracle"

    def prepare(self):
        """
        测试任务准备  实现测试任务的测试准备工作自动化，比如打开浏览器
        :return:
        """
        variable = Variable()
        system_env = variable.get_variable('varg_db_type')
        if system_env and system_env != 'AGENT_EMPTY':
            self.browser_id = "env_{}".format(system_env)
            self.is_blade_flag = True
            self.init_env = system_env
        else:
            self.browser_id = "primary"
            self.is_blade_flag = False
        logger.info(f'web_config: {web_config}')
        system_odbc_config = SysConfig.get(self.init_env).get('databases')
        system_odbc_config.update({'env': self.init_env})
        logger.info(f'Init db link! system_odbc_config: {system_odbc_config}')
        DB_APP.make_db(**system_odbc_config)
        if self.is_blade_flag:
            WebDriverFactory(browser_id=self.browser_id)
            # login_web_page()    # 首先登录

    def setup(self):
        """
        测试用例准备
        :return:
        """
        pass

    def teardown(self):
        """
        测试用例结束
        :return:
        """
        pass

    def cleanup(self):
        """
        测试任务结束  实现测试任务的测试结束清理工作自动化，比如关闭浏览器
        :return:
        """
        if DB_APP.dba:
            DB_APP.close_link()
        if self.is_blade_flag:
            WebDriverFactory(browser_id=self.browser_id).close()

